package com.function;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Mail {
//	private static String d_email = "nccudruk@gmail.com", d_password = "nccumis10",
//			d_host = "smtp.gmail.com", d_port = "465",m_subject = "Testing";
			//m_text = "Hey, this is the testing email using smtp.gmail.com.";

//	public static void main(String[] args)
//    {
//                String[] to={"96306096@nccu.edu.tw","96306090@nccu.edu.tw","96306091@nccu.edu.tw","96306092@nccu.edu.tw"};
//                String[] cc={"96306096@nccu.edu.tw"};
//                String[] bcc={"96306096@nccu.edu.tw"};
//                This is for google
//                Mail.sendMail(d_email,d_password,d_host,d_port,"true",
//"true",true,"javax.net.ssl.SSLSocketFactory","false",to,cc,bcc,
//m_subject,m_text);             
//    }

	public synchronized static boolean sendMail(String userName,
			String passWord, String host, String port, String starttls,
			String auth, boolean debug, String socketFactoryClass,
			String fallback, String recipients, String cc, String bcc,
			String subject, String text) {
		Properties props = new Properties();
		// Properties props=System.getProperties();
		props.put("mail.smtp.user", userName);
		props.put("mail.smtp.host", host);
		if (!"".equals(port))
			props.put("mail.smtp.port", port);
		if (!"".equals(starttls))
			props.put("mail.smtp.starttls.enable", starttls);
		props.put("mail.smtp.auth", auth);
		if (debug) {
			props.put("mail.smtp.debug", "true");
		} else {
			props.put("mail.smtp.debug", "false");
		}
		if (!"".equals(port))
			props.put("mail.smtp.socketFactory.port", port);
		if (!"".equals(socketFactoryClass))
			props.put("mail.smtp.socketFactory.class", socketFactoryClass);
		if (!"".equals(fallback))
			props.put("mail.smtp.socketFactory.fallback", fallback);

		try {
			Session session = Session.getDefaultInstance(props, null);
			session.setDebug(debug);
			MimeMessage msg = new MimeMessage(session);
			msg.setText(text);
			msg.setSubject(subject);
			msg.setFrom(new InternetAddress("p_sambasivarao@sutyam.com"));
//			for (int i = 0; i < recipients.length; i++) {
				msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
						recipients));
//			}
//			for (int i = 0; i < cc.length; i++) {
				msg.addRecipient(Message.RecipientType.CC, new InternetAddress(
						cc));
//			}
//			for (int i = 0; i < bcc.length; i++) {
				msg.addRecipient(Message.RecipientType.BCC,
						new InternetAddress(bcc));
//			}
			msg.saveChanges();
			Transport transport = session.getTransport("smtp");
			transport.connect(host, userName, passWord);
			transport.sendMessage(msg, msg.getAllRecipients());
			transport.close();
			return true;
		} catch (Exception mex) {
			mex.printStackTrace();
			return false;
		}
	}

}
